<template>
  <div class="view-wrapper">
    <div class="head">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="文章分类名称：">
          <el-input placeholder="请输入文章分类" v-model="formInline.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <el-button type="primary" @click="addBuy">新建文章分类</el-button>
      </div>
      <ex-table ref="table" :loading="loading" :table-column="tableColumn" :table-data="tableData"
        @pageSizeChange="handleSizeChange" @currentPageChange="handleCurrentChange">
        <template v-slot:imgOperate="{ scope }">
          <div>
            <el-image style="width: 150px; height: 100px;margin-right: 10px" :src="scope.row.imgUrl"
              fit="scale-down" :preview-src-list="scope.row.srcList"></el-image>
            <el-image style="width: 150px; height: 100px" :src="scope.row.imgUrl" fit="scale-down"
              :preview-src-list="scope.row.srcList"></el-image>
          </div>
        </template>
        <template v-slot:operate="{ scope }">
          <el-button type="primary" size="small" @click="editJump(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="small" @click="deactivate(scope.row.id)">删除</el-button>
        </template>
      </ex-table>
    </div>
    <!-- 新增、编辑 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="50%" :close-on-click-modal="false">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm"
        v-loading="dialogLoading">
        <el-form-item label="文章分类名称：" prop="name" :label-width="labelWidth">
          <el-input v-model="ruleForm.name" placeholder="请输入" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="排序：" prop="sort" :label-width="labelWidth">
          <el-input v-model="ruleForm.sort" placeholder="请输入" style="width:300px"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editForm('ruleForm')" :loading="AgreeDisable">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  // components
  import {
    articleTypeLs,
    addArticleType,
    editArticleType,
    articleTypeDetail,
    deleteArticleType
  } from "@/api/officialWebsite";
  import ExportExcelButton from "@/components/ExportExcelButton/index";
  import handlePageMixin from "@/mixins/handlePageMixin";
  import {
    mapGetters
  } from "vuex";
  export default {
    name: "billList",
    components: {
      ExportExcelButton,
    },
    mixins: [handlePageMixin],
    data() {
      return {
        AgreeDisable: false,
        loading: false,
        dialogLoading: false,
        dialogId: '', //编辑id
        formInline: {
          name: "", //分类名称
        },
        labelWidth: '150px',
        dialogVisible: false,
        tableData: [],
        tableColumn: [{
            label: "文章分类名称",
            prop: "name",
          },
          {
            label: "排序",
            prop: "sort",
            width: 150
          },
          {
            label: "操作",
            slot: true,
            slotName: "operate",
            width: 240,
            fixed: "right",
          },
        ],
        ruleForm: {
          name: "", //文章分类名称
          sort: "", //排序
        },
        rules: {
          name: [{
            required: true,
            message: "请输入文章分类名称",
            trigger: ['change', 'bulr'],
          }],
          sort: [{
            required: true,
            message: "请输入排序",
            trigger: ['change', 'bulr'],
          }],
        },
      };
    },
    computed: {
      ...mapGetters(["isChain"]),
    },
    created() {},
    mounted() {
      this.getList();
    },
    methods: {
      addBuy() {
        //新增
        this.dialogId = null
        this.ruleForm.name = ''
        this.ruleForm.sort = ''
        this.dialogVisible = true;
        setTimeout(() => {
          this.$refs['ruleForm'].resetFields();
        }, 1)
      },
      editJump(id) {
        //编辑
        this.dialogId = id
        this.dialogVisible = true;
        this.getBannerInfo()
        setTimeout(() => {
          this.$refs['ruleForm'].resetFields();
        }, 1)
      },
      deactivate(id) {
        //删除
        this.$confirm("是否删除？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            deleteArticleType(id).then((res) => {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
              });
              setTimeout(() => {
                this.reset();
              }, 800);
            });
          })
          .catch(() => {

          });
      },
      search() {
        //搜索
        console.log(this.formInline);
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      reset() {
        this.formInline = {};
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      getBannerInfo() { //获取信息详情
        this.dialogLoading = true
        articleTypeDetail(this.dialogId).then((res) => {
            let info = res.result
            this.ruleForm.name = info.name
            this.ruleForm.sort = info.sort
            this.dialogLoading = false
          })
          .finally(() => {
            this.dialogLoading = false;
          });
      },
      getList() {
        const params = {
          ...this.pageOptions,
          ...this.formInline,
        };
        this.loading = true;
        articleTypeLs(params)
          .then((res) => {
            const {
              total,
              records
            } = res.result;
            this.tableData = records;
            this.$refs.table.pageOptions.total = total;
          })
          .finally(() => {
            this.loading = false;
          });
      },
      editForm(formName) {
        //新增编辑确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.AgreeDisable = true
            if (this.dialogId == null) { //新增
              var params = {
                ...this.ruleForm,
              };
              addArticleType(params).then((res) => {
                  this.$message({
                    message: "操作成功",
                    type: "success",
                  });
                  setTimeout(() => {
                    this.reset();
                    this.dialogVisible = false;
                    this.AgreeDisable = false;
                  }, 1000);
                })
                .finally(() => {
                  this.AgreeDisable = false;
                });
            } else { //编辑
              var params = {
                id: this.dialogId,
                ...this.ruleForm,
              };
              editArticleType(params).then((res) => {
                  this.$message({
                    message: "操作成功",
                    type: "success",
                  });
                  setTimeout(() => {
                    this.reset();
                    this.dialogVisible = false;
                    this.AgreeDisable = false;
                  }, 1000);
                })
                .finally(() => {
                  this.AgreeDisable = false;
                });
            }
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .el-card-title {
    font-size: 20px;
  }

  .head {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px 0;
  }

  .el-form--inline .el-form-item {
    margin-right: 40px;
  }

  .listBox {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px;
    margin-top: 10px;
  }

  .table_list {
    display: flex;
    align-items: center;
    // border: 1px solid #DCDFE6;
    border-radius: 7px;
    width: 220px;
    margin-bottom: 10px;
  }

  .tableBox {
    width: calc(100% / 3);
    height: 36px;
    text-align: center;
    line-height: 36px;
    color: #303133;
    font-size: 14px;
    border: 1px solid #dcdfe6;
    border-right: 0;
  }

  .tableBox:first-child {
    border-radius: 7px 0px 0px 7px;
  }

  .tableBox:last-child {
    border-right: 1px solid #dcdfe6;
    border-radius: 0px 7px 7px 0px;
  }

  .spchoosetext {
    background-color: #fff4e6;
    color: #ff9300;
    // border: 1px solid #ff9300;
  }

  .listBox_head {
    margin-bottom: 20px;
    display: flex;
  }

  .sumit_button {
    color: #fff !important;
    background: #ff7b35 !important;
    border: 0;
  }

  .sumit_button:hover {
    opacity: 0.9;
  }

  .banner_img {
    width: 200px;
  }

</style>
